
/*验证手机号的正则表达式*/
function checkPhone(phone){
   var phone = phone;
   if(!(/^1[34578]\d{9}$/.test(phone))){
      return false;
   }else{
      return true;
   }
};
/*验证邮箱*/
function CheckMail(mail) {
   var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
   if (filter.test(mail)){
      return true;
   }else {
      return false;
   }
};
/*验证邮箱*/
function checkPassword(password) {
  var filter  = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,20}$/;
  if (filter.test(password)){
    return true;
  }else {
    return false;
  }
};
/*字符串加密*/
/* var code = kbt.encrypt('我爱北京天安门');
* alert(code);
* 解密
* var str =  kbt.decrypt(code);
* alert(str);
*/
kbt = (function () {
   var that = {};
   function encrypt(str, pwd) {
      if(str == '') {
         return '';
      }
      str = encodeURIComponent(str);
      if(!pwd || pwd == '') {
         pwd = 'kb1234';
      }
      pwd = encodeURIComponent(pwd);
      if(pwd == '' || pwd.length <= 0) {
         return '';
      }
      var prand = '';
      for(var i = 0, len = pwd.length; i < len; i += 1) {
         prand += pwd.charCodeAt(i).toString();
      }
      var sPos = Math.floor(prand.length / 5);
      var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
          prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
      var incr = Math.ceil(pwd.length / 2);
      var modu = Math.pow(2, 31) - 1;
      if(mult < 2) {
         return '';
      }
      var salt = Math.round(Math.random() * 1000000000) % 100000000;
      prand += salt;
      while(prand.length > 10) {
         prand = (parseInt(prand.substring(0, 10)) +
         parseInt(prand.substring(10, prand.length))).toString();
      }
      prand = (mult * prand + incr) % modu;
      var encChr = '';
      var encStr = '';
      for(var i = 0, len = str.length; i < len; i += 1) {
         encChr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
         if(encChr < 16) {
            encStr += '0' + encChr.toString(16);
         }else{
            encStr += encChr.toString(16);
         }
         prand = (mult * prand + incr) % modu;
      }
      salt = salt.toString(16);
      while(salt.length < 8) {
         salt = "0" + salt;
      }
      encStr += salt;
      return encStr;
   }
   function decrypt(str, pwd) {
      if(str == ''){
         return '';
      }
      if(!pwd || pwd == ''){
         pwd = 'kb1234';
      }
      pwd = encodeURIComponent(pwd);
      if(str == undefined || str.length < 8) {
         return '';
      }
      if(pwd == undefined || pwd.length <= 0) {
         return '';
      }
      var prand = '';
      for(var i = 0, len = pwd.length; i < len; i += 1) {
         prand += pwd.charCodeAt(i).toString();
      }
      var sPos = Math.floor(prand.length / 5);
      var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) +
          prand.charAt(sPos * 4) + prand.charAt(sPos * 5));
      var incr = Math.round(pwd.length / 2);
      var modu = Math.pow(2, 31) - 1;
      var salt = parseInt(str.substring(str.length - 8, str.length), 16);
      str = str.substring(0, str.length - 8);
      prand += salt;
      while(prand.length > 10) {
         prand = (parseInt(prand.substring(0, 10)) +
         parseInt(prand.substring(10, prand.length))).toString();
      }
      prand = (mult * prand + incr) % modu;
      var encChr = '';
      var encStr = '';
      for(var i = 0, len = str.length; i < len; i += 2) {
         encChr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
         encStr += String.fromCharCode(encChr);
         prand = (mult * prand + incr) % modu;
      }
      return decodeURIComponent(encStr);
   }
   that.encrypt = encrypt;
   that.decrypt = decrypt;
   return that;
})();


/**
 * 日期格式化
 * @param fmt
 * @returns {*}
 */
Date.prototype.pattern=function(fmt) {
    var o = {
        "M+" : this.getMonth()+1, //月份
        "d+" : this.getDate(), //日
        "h+" : this.getHours()%12 == 0 ? 12 : this.getHours()%12, //小时
        "H+" : this.getHours(), //小时
        "m+" : this.getMinutes(), //分
        "s+" : this.getSeconds(), //秒
        "q+" : Math.floor((this.getMonth()+3)/3), //季度
        "S" : this.getMilliseconds() //毫秒
    };
    var week = {
        "0" : "/u65e5",
        "1" : "/u4e00",
        "2" : "/u4e8c",
        "3" : "/u4e09",
        "4" : "/u56db",
        "5" : "/u4e94",
        "6" : "/u516d"
    };
    if(/(y+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    if(/(E+)/.test(fmt)){
        fmt=fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[this.getDay()+""]);
    }
    for(var k in o){
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
}

// var date = new Date();
// window.alert(date.pattern("yyyy-MM-dd hh:mm:ss"));
/*js 正则验证只输入数字*/
function validate(value){
    var reg = new RegExp("^[0-9]*$");
    if(!reg.test(value)){
        return false;
    }else if(!/^[0-9]*$/.test(value)){
        return false;
    }else{
        return true;
    }
}

//设置当前用户的头像
function portraitsetting(userPortrait,name) {
    var nameSub="";
    //判断是否是默认图片
    if (userPortrait == "img/user_info_head.png"){
        if (name != null || name != "") {
            if (isChinese(name)) {
                nameSub = name.substr(name.length - 2);
            } else {
                nameSub = name.substr(name.length - 5);
            }
        }
    }
    return  nameSub;
}


/**
 * 判断是否为中文
 * @param temp
 * @returns {boolean}
 */
function isChinese(temp) {
    var re = /[^\u4e00-\u9fa5]/;
    if (re.test(temp)) return false;
    return true;
}

/**
 * 获取当前时间
 * @returns {时间}
 */
function getNowFormatDate() {
  var date = new Date();
  var seperator1 = "-";
  var year = date.getFullYear();
  var month = date.getMonth() + 1;
  var strDate = date.getDate();
  if (month >= 1 && month <= 9) {
    month = "0" + month;
  }
  if (strDate >= 0 && strDate <= 9) {
    strDate = "0" + strDate;
  }
  var currentdate = year + seperator1 + month + seperator1 + strDate + "";
  return currentdate;
}




